package com.pphh.demo.amqp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Please add description here.
 *
 * @author huangyinhuang
 * @date 2019/3/27
 */
@Service
public class MqConsumer {

    private final static Logger log = LoggerFactory.getLogger(MqConsumer.class);

    @Autowired
    private AmqpTemplate amqpTemplate;

    public String getMessage() {
        String msg = null;

        log.info("try to pull a message from rabbit mq");
        Message message = amqpTemplate.receive("demo.queues.test.pull");
        if (message != null) {
            try {
                msg = new String(message.getBody());
                log.info("received a message from mq, msg = {}", msg);
                Helper.logMessageProperties(message.getMessageProperties());
            } catch (Exception e) {
                log.error("异常消息，exception = {}", e.getMessage());
            }
        }
        return msg;
    }
}
